package btree;

import java.io.IOException;

import global.*;

public class BTFileScan {

	private RID current;
	private RID next;
	private boolean closed = false;
	private BTLeafPage leaf;
	private KeyClass low_key, high_key;

	public BTFileScan() {

	}

	public void setCurrent() {
		if (low_key == null) {
			PageId ID;
			try {
				ID = BTreeFile.getHeaderPage().get_rootId();

				BTSortedPage page;
				page = new BTSortedPage(ID, BTreeFile.getHeaderPage()
						.get_keyType());
				page.dumpPage();
				boolean stop = false;
				while (page.getType() != NodeType.LEAF && !stop) {
					PageId left = ((BTIndexPage) page).getLeftLink();
					if (left != null) {
						SystemDefs.JavabaseBM
								.unpinPage(page.getCurPage(), true);
						SystemDefs.JavabaseBM.pinPage(left, page, false);
						page.dumpPage();

					} else
						stop = true;

				}
				if (!stop) {
					leaf = (BTLeafPage) page;
					low_key = leaf.getFirst(current).key;
					next = current;
				} else {
					// /////throw exception
				}
			} catch (Exception e) {
				e.printStackTrace();
			}

		} else {
	//		leaf = BTreeFile.search(low_key);
			try {
				current = leaf.findRID(low_key);
				next = current;
			} catch (KeyNotMatchException e) {
				e.printStackTrace();
			}
		}
	}

	public KeyDataEntry get_next() {
		if (closed == false) {
			try {
				if (current != null) {
					current = next;
					next = leaf.nextRecord(next);
				} else {
					PageId nextPageId = leaf.getNextPage();
					if (nextPageId.pid != -1) {
						SystemDefs.JavabaseBM
								.unpinPage(leaf.getCurPage(), true);
						SystemDefs.JavabaseBM.pinPage(nextPageId, leaf, false);
						leaf.dumpPage();
						leaf.getFirst(current);
						next = current;
						next = leaf.nextRecord(next);

					} else
						return null;
				}
				KeyDataEntry returned = leaf.getCurrent(current);
				if (high_key != null
						&& BT.keyCompare(high_key, returned.key) < 0) {
					return null;
				} else
					return returned;

			} catch (Exception e) {
				return null;
			}
		} else
			return null;
	}

	public void delete_current() {
		if (current != null && closed == false) {
			KeyDataEntry dataEntry = leaf.getCurrent(current);
			try {
				leaf.delEntry(dataEntry);
			} catch (Exception e) {
				e.printStackTrace();

			}
		}

		// ///////////////////////Scan is closed
	}

	public int keysize() {
		if (closed == false) {
			try {
				return BTreeFile.getHeaderPage().get_maxKeySize();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return -1;
		// //////////////////////////////////////////////Scan is closed
	}

	public void DestroyBTreeFileScan() {
		if (closed == false)
			closed = true;
		// /////////////////////////////////////////////Scan is closed
	}

	public KeyClass getLow_key() {
		return low_key;
	}

	public void setLow_key(KeyClass low_key) {
		this.low_key = low_key;
	}

	public KeyClass getHigh_key() {
		return high_key;
	}

	public void setHigh_key(KeyClass high_key) {
		this.high_key = high_key;
	}

}
